package com.xxx;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class MovieMethod {
    List<User> users = new ArrayList<>();
    List<Business> businesses = new ArrayList<>();
    List<Movie> movies = new ArrayList<>();
    List<User> userlogin = new ArrayList<>();
    List<Business> businesslogin = new ArrayList<>();
    Scanner sc = new Scanner(System.in);
    public void start(){
        try(
                ObjectInputStream uois = new ObjectInputStream(new FileInputStream("JavaLs07\\user.txt"));
                ObjectInputStream bois = new ObjectInputStream(new FileInputStream("JavaLs07\\business.txt"));
                ObjectInputStream mois = new ObjectInputStream(new FileInputStream("JavaLs07\\movies.txt"));
                )
              {
            users = (ArrayList<User>) uois.readObject();
            businesses = (ArrayList<Business>) bois.readObject();
            movies = (ArrayList<Movie>) mois.readObject();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        while (true) {
            System.out.println("==========电影首页==========");
            System.out.println("1.登录");
            System.out.println("2.用户注册");
            System.out.println("3.商家注册");
            System.out.println("请输入操作命令:");
            int temp = sc.nextInt();
            switch (temp){
                case 1:
                    login();
                    break;
                case 2:
                    useradd();
                    break;
                case 3:
                    businessadd();
                    break;
                default:
                    System.out.println("您输入的指令不存在请重新确认!~~");
                    break;
            }
        }
    }
    private void login() {
        if (users.size()<1&&businesses.size()<1){
            System.out.println("当前没有账户!");
            return;
        }
        while (true) {
            int judg = judgment();
            if (judg==1){//用户
                System.out.println("========您是用户========");
                if (movies.size()<1){
                    System.out.println("当前没有任何电影请先交商家加点电影");
                    return;
                }
                System.out.println("全部的电影如下:");
                for (int i = 0; i < movies.size(); i++) {
                    Movie movie = movies.get(i);
                    System.out.print(movie.getMid()+"  ");
                    System.out.print(movie.getMname()+"  ");
                    System.out.print(movie.getScore()+"  ");
                    System.out.println(movie.getMmoney());
                    System.out.println("------------------------------------");
                }
                while (true) {
                    System.out.println("您要买哪张票请输入该票的id:");
                    String mid = sc.next();
                    for (int i = 0; i < movies.size(); i++) {
                        Movie movie = movies.get(i);
                       if (movie.getMid().equals(mid)){
                           User user = userlogin.get(0);
                               if (user.getUmoney()<movie.getMmoney()){
                                   System.out.println("您的账户没有钱!!!");
                                   System.out.println("是否要充钱?y/n");
                                   String yn = sc.next();
                                   switch (yn){
                                       case "y":
                                           System.out.println("请输入金额:");
                                           Double umoney = sc.nextDouble();
                                           String uid = user.getUid();
                                           for (User user1 : users) {
                                               if (user1.getUid().equals(uid)){
                                                   user1.setUmoney(user1.getUmoney()+umoney);
                                                   System.out.println("充值成功!");
                                                   System.out.print("您当前金额是:");
                                                   System.out.println(user1.getUmoney());
                                                   break;
                                               }
                                           }
                                           break;
                                       case "n":
                                           System.out.println("好的欢迎下次光临!~~~");
                                           return;
                                   }
                               }
                               System.out.println("购票成功!~~~");
                               for (User user1 : users) {
                                   if (user1.getUid().equals(user.getUid())){
                                       user1.setUmoney(user1.getUmoney()-movie.getMmoney());
                                       System.out.println("请为该片打一下分吧!");
                                       double score = sc.nextDouble();
                                       movie.setScore(score);
                                       System.out.println("好的欢迎下次光临!~~~");
                                       try (
                                               ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("JavaLs07\\user.txt"));
                                               ObjectOutputStream moos = new ObjectOutputStream(new FileOutputStream("JavaLs07\\movies.txt"));
                                       ){
                                           oos.writeObject(users);
                                           moos.writeObject(movies);
                                       } catch (Exception e) {
                                           e.printStackTrace();
                                       }
                                       return;
                                   }
                               }
                           }
                       }
                    System.out.println("您输入的id不存在");
                }
            }else if (judg==2){//商家
                System.out.println("========您是商家========");
                if (movies.size()<1){
                    System.out.println("当前没有任何电影");
                }else {
                    System.out.println("全部的电影信息如下:");
                    for (int i = 0; i < movies.size(); i++) {
                        Movie movie = movies.get(i);
                        System.out.print(movie.getMid()+"  ");
                        System.out.print(movie.getMname()+"  ");
                        System.out.print(movie.getScore()+"  ");
                        System.out.println(movie.getMmoney());
                        System.out.println("------------------------------------");
                    }
                }
                while (true) {
                    System.out.println("请输入您的命令!");
                    System.out.println("1.上架影片:");
                    System.out.println("2.下架影片:");
                    System.out.println("3.修改影片信息:");
                    System.out.println("4.退出!");
                    int command = sc.nextInt();
                    switch (command){
                        case 1:
                            addMovie();
                            break;
                        case 2:
                            removeMovie();
                            break;
                        case 3:
                            reviseMovie();
                            break;
                        case 4:
                            return;
                        default:
                            System.out.println("您输入的指令不存在!");
                    }
                }

            }else if (judg==3){
                System.out.println("您输入的账号密码有误!请确认一下!~~~");
            }
        }
    }

    private void reviseMovie() {
        if (movies.size()<1){
            System.out.println("当前没有任何电影!");
            return;
        }
        System.out.println("全部的电影信息如下:");
        for (int i = 0; i < movies.size(); i++) {
            Movie movie = movies.get(i);
            System.out.print(movie.getMid()+"  ");
            System.out.print(movie.getMname()+"  ");
            System.out.print(movie.getScore()+"  ");
            System.out.println(movie.getMmoney());
            System.out.println("------------------------------------");
        }
        while (true) {
            System.out.println("请输入您要修改的电影id:");
            String mid = sc.next();
            for (int i = 0; i < movies.size(); i++) {
                Movie movie = movies.get(i);
                if (movie.getMid().equals(mid)){
                    System.out.println("请输入该电影的新名字");
                    String mname = sc.next();
                    movie.setMname(mname);
                    System.out.println("请输入该电影的新价格");
                    double mmoney = sc.nextDouble();
                    movie.setMmoney(mmoney);
                    movie.setScore(0.0);
                    try (
                            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("JavaLs07\\movies.txt"));
                    ){
                        oos.writeObject(movies);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return;
                }else {
                    System.out.println("没有该电影!");
                }
            }
        }
    }

    private void removeMovie() {
        if (movies.size()<1){
            System.out.println("当前没有任何电影!");
            return;
        }
        System.out.println("全部的电影信息如下:");
        for (int i = 0; i < movies.size(); i++) {
            Movie movie = movies.get(i);
            System.out.print(movie.getMid()+"  ");
            System.out.print(movie.getMname()+"  ");
            System.out.print(movie.getScore()+"  ");
            System.out.println(movie.getMmoney());
            System.out.println("------------------------------------");
        }

        while (true) {
            System.out.println("请输入您要下架的电影id:");
            String mid = sc.next();
            for (int i = 0; i < movies.size(); i++) {
                Movie movie = movies.get(i);
                if (movie.getMid().equals(mid)){
                    movies.remove(movie);
                    System.out.println("下架成功!");
                    try (
                            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("JavaLs07\\movies.txt"));
                    ){
                        oos.writeObject(movies);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return;
                }else {
                    System.out.println("没有该电影!");
                }
            }
        }
    }

    private void addMovie() {
        Movie movie = new Movie();
        while (true) {
            System.out.println("请输入该影片的id:");
            String mid = sc.next();
            for (int i = 0; i < movies.size(); i++) {
                Movie m = movies.get(i);
                if (m.getMid().equals(mid)){
                    System.out.println("该id重复请重新输入!");
                }
            }
            movie.setMid(mid);
            break;
        }
        System.out.println("请输入该影片的名字:");
        String mname = sc.next();
        movie.setMname(mname);
        System.out.println("请输入该影片售卖的金额:");
        Double mmoney = sc.nextDouble();
        movie.setMmoney(mmoney);
        movie.setScore(0.0);
        movies.add(movie);
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("JavaLs07\\movies.txt"));
        ){
            oos.writeObject(movies);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void businessadd() {
        Business business = new Business();
        System.out.println("请输入账号:");
        String bid = sc.next();
        business.setBid(bid);
        System.out.println("请输入姓名:");
        String bname = sc.next();
        business.setBname(bname);
        while (true) {
            System.out.println("请您输入性别");
            String bsex = sc.next();
            if (bsex.equals("男")||bsex.equals("女")){
                business.setBsex(bsex.charAt(0));
                break;
            }else {
                System.out.println("你输入的不符合规范请重新输入!~~");
            }
        }
        System.out.println("请输入手机号:");
        String bphone = sc.next();
        business.setBphone(bphone);
        System.out.println("请输入账号密码:");
        String bpassword = sc.next();
        business.setBpassword(bpassword);
        while (true) {
            System.out.println("请您输入存储金额!~~");
            Double bmoney = sc.nextDouble();
            if (bmoney<100){
                System.out.println("您存储的金额过小!(最小金额为100)");
            }else {
                business.setBmoney(bmoney);
                break;
            }
        }
        System.out.println("请输入店名:");
        String shopname = sc.next();
        business.setShopname(shopname);
        System.out.println("请输入店铺地址:");
        String address = sc.next();
        business.setAddress(address);
        System.out.println("添加成功!");
        businesses.add(business);
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("JavaLs07\\business.txt"));
        ){
            oos.writeObject(businesses);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void useradd() {
        User user = new User();
        System.out.println("请输入账号!:");
        String uid = sc.next();
        user.setUid(uid);
        System.out.println("请您输入姓名");
        String uname = sc.next();
        user.setUname(uname);
        while (true) {
            System.out.println("请您输入性别");
            String usex = sc.next();
            if (usex.equals("男")||usex.equals("女")){
                user.setUsex(usex.charAt(0));
                break;
            }else {
                System.out.println("你输入的不符合规范请重新输入!~~");
            }
        }
        System.out.println("请您输入手机号!~~");
        String uphone = sc.next();
        user.setUphone(uphone);
        System.out.println("请您输入账户密码!~~");
        String upassword = sc.next();
        user.setUpassword(upassword);
        while (true) {
            System.out.println("请您输入存储金额!~~");
            Double umoney = sc.nextDouble();
            if (umoney<100){
                System.out.println("您存储的金额过小!(最小金额为100)");
            }else {
                user.setUmoney(umoney);
                break;
            }
        }
        System.out.println("恭喜您添加成功!~~");
        users.add(user);
        for (int i = 0; i < users.size(); i++) {
            User user1 = users.get(i);
            System.out.print(user1.getUid()+"  ");
            System.out.print(user1.getUname()+"  ");
            System.out.print(user1.getUsex()+"  ");
            System.out.print(user1.getUphone()+"  ");
            System.out.print(user1.getUpassword()+"  ");
            System.out.println(user1.getUmoney()+"  ");
        }
        try (
                ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("JavaLs07\\user.txt"));
        ){
        oos.writeObject(users);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private int judgment(){
        System.out.println("请输入您的账户!");
        String id = sc.next();
        System.out.println("请输入您的密码!");
        String password = sc.next();
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            String uid = user.getUid();
            String upassword = user.getUpassword();
            if (uid.equals(id)&&upassword.equals(password)){
                userlogin.add(user);
               return 1;//用户
            }
        }
        for (int i = 0; i < businesses.size(); i++) {
            Business business = businesses.get(i);
            String bid = business.getBid();
            String bpassword = business.getBpassword();
            if (bid.equals(id)&&bpassword.equals(password)){
                businesslogin.add(business);
                return 2;//商家
            }
        }
        return 3;//错误
    }
}
